package com.thp.activeMQ;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;

import org.apache.activemq.ActiveMQConnectionFactory;

public class TopicProducer {
public static void main(String[] args) throws JMSException {
		
		// 1.创建工厂  ActiveMQConnection
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.129:61616");
		// 2.获取连接  ActiveMQSession
		Connection connection = connectionFactory.createConnection();
		System.out.println( "connection "+ connection.getClass());
		// 3.启动连接
		connection.start();
		// 4.获取session  (参数1:是否启动事务   参数2:消息确认模式)
		Session  session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);  // 自动确认
		System.out.println("session: " + session.getClass());
		// 5.创建主题对象  ActiveMQTopic
		Topic topic = session.createTopic("test-topic");
		System.out.println("topic: " + topic.getClass());
		// 6.创建消息生产者  ActiveMQMessageProducer
		MessageProducer producer = session.createProducer(topic);
		System.out.println("producer: " + producer.getClass());
		// 7.创建消息  ActiveMQTextMessage
		TextMessage message = session.createTextMessage("暑去冬来杀人刀,斩尽世上的男女老少");
		System.out.println("message: " + message.getClass());
		// 8.发送消息
		producer.send(message);
		// 9.关闭资源
		producer.close();
		session.close();
		connection.close();
	}
}
